package com.rdtravel.services;

import com.rdtravel.entities.Partner;

import javax.persistence.NoResultException;
import javax.persistence.TypedQuery;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
@Transactional
class JPAPartnersService extends JPAService implements PartnersService {

	@Override
	public Partner findByName(String name) {
		if (name.isEmpty()) {
			throw new IllegalArgumentException("name is empty");
		}
		TypedQuery<Partner> query = entityManager.createNamedQuery(
				"findPartnersByName", Partner.class);
		query.setParameter("name", name);
		try {
			return query.getSingleResult();
		} catch (NoResultException ex) {
			return null;
		}
	}
}
